package com.example.smartcity_40.Outpatient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity_40.Outpatient.bean.StatusBean;
import com.example.smartcity_40.Outpatient.bean.VisitorBean;
import com.example.smartcity_40.R;
import com.example.smartcity_40.tool.Tool;
import com.google.gson.Gson;

import org.json.JSONObject;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class ModifyVisitorActivity extends AppCompatActivity {

    private ModifyVisitorActivity context;
    private String TAG = "TAG";
    private VisitorBean.RowsBean rowsBean;
    private EditText ed_name;
    private RadioGroup sex_option;
    private EditText ed_address;
    private EditText ed_cardId;
    private EditText ed_tel;
    private RadioButton sex_male;
    private RadioButton sex_female;
    private TextView save;
    private Tool tool;
    private String SEX = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_visitor);
        context = this;
        initview();
        initpenson();
    }

    private void initpenson() {
        try {
            String name = rowsBean.getName();
            if (!TextUtils.isEmpty(name)) {
                ed_name.setText(name);
            }

            String address = rowsBean.getAddress();
            if (!TextUtils.isEmpty(address)) {
                ed_address.setText(address);
            }
            String cardId = rowsBean.getCardId();
            if (!TextUtils.isEmpty(cardId)) {
                ed_cardId.setText(cardId);
            }
            String tel = rowsBean.getTel();
            if (!TextUtils.isEmpty(tel)) {
                ed_tel.setText(tel);
            }


            String sex = rowsBean.getSex();

            if ("0".equals(sex)) {
                sex_male.setChecked(true);
                sex_female.setChecked(false);
            } else if ("1".equals(sex)) {
                sex_male.setChecked(false);
                sex_female.setChecked(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initview() {
        ImageView back = findViewById(R.id.back);
        ImageView addVisitor = findViewById(R.id.addVisitor);

        Intent intent = getIntent();
        String hospitalbean = intent.getStringExtra("hospitalbean");
        String hospitalid = intent.getStringExtra("hospitalid");
        Gson gson = new Gson();
        rowsBean = gson.fromJson(hospitalbean, VisitorBean.RowsBean.class);


        ed_name = findViewById(R.id.ed_name);
        sex_option = findViewById(R.id.sex_option);
        ed_address = findViewById(R.id.ed_address);
        // TextView ed_birthday = findViewById(R.id.ed_birthday);
        ed_cardId = findViewById(R.id.ed_cardId);
        ed_tel = findViewById(R.id.ed_tel);

        sex_male = findViewById(R.id.sex_male);
        sex_female = findViewById(R.id.sex_female);
        save = findViewById(R.id.save);

        JSONObject jsonObject = new JSONObject();
        tool = new Tool(context);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });


        addVisitor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OutpatientTypeActivity.class);
                context.startActivity(intent);
            }
        });



        sex_option.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int checkedRadioButtonId = group.getCheckedRadioButtonId();
                if (R.id.sex_male == checkedRadioButtonId) {
                    SEX = "0";
                } else if (R.id.sex_female == checkedRadioButtonId) {
                    SEX = "1";
                }
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String name = ed_name.getText().toString();
                    String address = ed_address.getText().toString();
                    //yyyy-MM-dd
                  //  String birthday = ed_birthday.getText().toString();
                    String cardId = ed_cardId.getText().toString();
                    String tel = ed_tel.getText().toString();

                    if (!TextUtils.isEmpty(name)
                            && !TextUtils.isEmpty(address)
                            && !TextUtils.isEmpty(SEX)
                            && !TextUtils.isEmpty(cardId)
                            && !TextUtils.isEmpty(tel)) {

                        jsonObject.put("name", name);
                        jsonObject.put("address", address);
                        //jsonObject.put("birthday", date);
                        jsonObject.put("cardId", cardId);
                        jsonObject.put("cardId", cardId);
                        jsonObject.put("sex", SEX);
                        jsonObject.put("id", hospitalid);

                        modifyvisitor(jsonObject.toString());
                    } else {
                        Toast.makeText(context, "填写的信息不能为空", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void modifyvisitor(String JSON) {
        tool.send("/prod-api/api/hospital/patient",
                Tool.PUT,
                JSON,
                true,
                StatusBean.class,
                new Function1<StatusBean, Unit>() {
                    @Override
                    public Unit invoke(StatusBean statusBean) {
                        if (statusBean.getCode() == 200) {
                            Toast.makeText(context, "修改成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, statusBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        return null;
                    }
                });
    }
}
